package com.msmall.community.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.message.FieldError;
import com.msmall.base.message.MessageManager;
import com.msmall.base.util.Constants;


public class FieldErrorTag extends TagSupport{

	private static final long serialVersionUID = -6506051135648454894L;
	
	private static final Log logger = LogFactory.getLog(FieldErrorTag.class);
	
	private boolean clear;
	
	private String filedName;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		HttpServletRequest req = (HttpServletRequest) super.pageContext.getRequest();
		HttpSession session = req.getSession();
		String info = "";
		try{
			Long userid = (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
			Map<String, FieldError> fieldErrors = MessageManager.getFieldErrors(userid);
			if(fieldErrors != null && !fieldErrors.isEmpty()){
				if(null != filedName && !"".equals(filedName)){
					FieldError fieldError = fieldErrors.get(filedName);
					info = fieldError.getMessage();
				}
			}
			writer.write(info);
			if(clear){
				MessageManager.clearFieldErrors(userid);
			}
		}catch(Exception e){
			
		}
		return (SKIP_BODY);
	}

	public boolean isClear() {
		return clear;
	}

	public void setClear(boolean clear) {
		this.clear = clear;
	}

	public String getFiledName() {
		return filedName;
	}

	public void setFiledName(String filedName) {
		this.filedName = filedName;
	}
}
